package com.rd.student.jike.class2.lihongming.Day0804;

import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 * @Auther:Heinrich
 * @Date:2023/8/4-08-04-11:14
 * @Description:com.rd.student.jike.class2.lihongming.Day0804
 * @version:1.0
 */
public class Cross2 {
    private static Semaphore semaphore = new Semaphore(3);

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println(Thread.currentThread().getName()+" 通过山洞");
                        
                        semaphore.release();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
        }
    }
}
